package com.le.tester.book.concurrentchallenge.atomicnoblocking15;

import com.le.tester.imooc.masterJUC.cas.SimulatedCAS;

/**
 * createTime：2025-05-24 10:10
 * description：基于cas实现的非阻塞计数器
 */
public class CASCounter {
    //    compare and swap
    public SimulatedCAS val;

    public int getVal() {
        return val.getValue();
    }

    public int increment() {
        int v;
        do {
            v = val.getValue();
        }
//        等待直到v==v+1
        while (v != val.compareAndSwap(v, v + 1));
        return v + 1;
    }
}
